package com.wawayaya.db.bookreader;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment;
import android.text.TextUtils;

import com.wawayaya.bookreader.bean.IntDouble;
import com.wawayaya.bookreader.bean.StringInt;
import com.wawayaya.bookreader.worddict.WordDictdescData;
import com.wawayaya.util.FileUtil;
import com.wawayaya.util.TLog;
import java.io.File;
import java.util.ArrayList;

public class DictionaryCNDB {

	private static final int DATABASE_VERSION = 1;
	public SQLiteDatabase m_dbInternal = null;

	private static final String TBNAME_LOCALDICTCACHE = "tb_dicttext";
	private static final String COLUMN_ID = "id";
	private static final String COLUMN_NETID = "netid";
	private static final String COLUMN_BOOKID = "bookid";
	private static final String COLUMN_WORD = "word";
	private static final String COLNAME_DICTNAME = "dictname";
    public static final String COLNAME_DICTINDEX = "dictindex";
	public static final String COLNAME_ITEMINDEX = "itemindex";
	public static final String COLNAME_TYPE = "type";
	public static final String COLNAME_TYPEVALUE = "typevalue";
	public static final String TYPENAME_WORDPINYIN = "wordpinyin";
    public static final String TYPENAME_PINYIN = "pinyin";
	public static final String TYPENAME_SHIYI = "shiyi";
	public static final String TYPENAME_CIZU = "cizu";
	public static final String TYPENAME_CIZUJIESHI = "cizujieshi";
	public static final String TYPENAME_LIJU = "liju";
	public static final String TYPENAME_LIJUJIESHI = "lijujieshi";
	public static final String TYPENAME_IMAGEURL = "imageurl";
    public static final String TYPENAME_DICTTYPE = "dicttype";
    public static final String TYPENAME_SOUNDURL = "soundurl";

		
	private static final String CREATE_SQL_LOCALDICTCACHE =
			"CREATE TABLE "+ TBNAME_LOCALDICTCACHE + "("+
				COLUMN_ID		+	" integer PRIMARY KEY AUTOINCREMENT," +
				COLUMN_NETID	+	" integer not null," +
				COLUMN_BOOKID	+	" integer not null," +
				COLUMN_WORD		+ 	" text not null," + 
				COLNAME_DICTNAME	+ 	" text," +
                COLNAME_DICTINDEX	+	" integer not null," +
				COLNAME_ITEMINDEX	+	" integer not null," +
				COLNAME_TYPE + " text not null," +
				COLNAME_TYPEVALUE + " text);";
	
	
	private static final String INSERT_SQL_LOCALDICTCACHE = "INSERT INTO "+
			TBNAME_LOCALDICTCACHE +" ("+
			COLUMN_ID + ","+
			COLUMN_NETID + ","+
			COLUMN_BOOKID + "," +
			COLUMN_WORD + "," +
			COLNAME_DICTNAME + "," +
            COLNAME_DICTINDEX	+ "," +
			COLNAME_ITEMINDEX + "," +
			COLNAME_TYPE + "," +
			COLNAME_TYPEVALUE + 
			") VALUES (NULL, %d, %d, \"%s\", \"%s\", %d, %d,\"%s\", \"%s\");";
	
	
	private static final String DELETE_SQL_WORDCACHEBYNETIDS = "DELETE FROM " +
			TBNAME_LOCALDICTCACHE + " WHERE " + COLUMN_NETID + "=%d AND " + COLUMN_WORD + "=\"%s\";";
	
	
	private static String m_sdictdbpath = null;
	
	public DictionaryCNDB(Context context) {
		// androi4.2 中引入了多用户机制，普通用户无法访问根目录下的/data/data目录，因而无法创建数据库文件
		// androi4.4 WRITE_EXTERNAL_STORAGE只为设备上的主要外部存储授予写权限，应用程序无法将数据写入二级外部存储设备
		// androi4.4 sd卡下只能在 sdcard/data/package-name 这个目录下创建数据库文件
		if(null != m_sdictdbpath) {
			try{
				DatabaseHelper dbHelper = new DatabaseHelper(context, m_sdictdbpath);
				if(null != dbHelper) {
					m_dbInternal = dbHelper.getWritableDatabase();
				}
			}catch(Exception e){
			}
		}
		if(null == m_dbInternal) {
			String sFilepath;
			try {
				/*//sFilepath = utils.getireadckclocaldictionarypath(utils.getparentfolder(BookShelfActivity.m_sbookzipsavedpath),false);
				DatabaseHelper dbHelper = new DatabaseHelper(context, sFilepath);
				if(null != dbHelper) {
					m_dbInternal = dbHelper.getWritableDatabase();
				}
				if(null != m_dbInternal){
					m_sdictdbpath = sFilepath;
				}
				*/
			} catch(android.database.sqlite.SQLiteCantOpenDatabaseException soe){
				try{
					sFilepath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/wawayaya.kids_iread/";
					new File(sFilepath).mkdirs();
					sFilepath += "lcdd.d";
					DatabaseHelper dbHelper = new DatabaseHelper(context, sFilepath);
					if(null != dbHelper) {
						m_dbInternal = dbHelper.getWritableDatabase();
					}
					if(null != m_dbInternal){
						m_sdictdbpath = sFilepath;
					}
				}catch(android.database.sqlite.SQLiteCantOpenDatabaseException soe2){
					TLog.log("DictionaryCNDB : dbBookShelfCoverImage : soe2 : " + soe2);
				}
			}
			catch (Exception e) {
				TLog.log("DictionaryCNDB : dbBookShelfCoverImage : e : " + e);
			}
			if(null == m_dbInternal) {
				String spath = context.getFilesDir().getAbsolutePath();
				if(!TextUtils.isEmpty(spath)){
					try{
						sFilepath = FileUtil.getireadckclocaldictionarypath(spath, false);
						DatabaseHelper dbHelper = new DatabaseHelper(context, sFilepath);
						if(null != dbHelper) {
							m_dbInternal = dbHelper.getWritableDatabase();
						}
						m_sdictdbpath = sFilepath;
					}catch(Exception e2){
						TLog.log("DictionaryCNDB : dbBookShelfCoverImage : e2 : " + e2);
					}
				}
			}
		}		
	}

	public void close() {
		m_sdictdbpath = null;
		if(null != m_dbInternal) {
			m_dbInternal.close();
			m_dbInternal = null;
		}
	}

	public void insertdictionarycache(WordDictdescData wdd) {
		if(null == m_dbInternal || null == wdd) {
			return;
		}
		if(null == wdd.m_dicts || wdd.m_dicts.length <= 0) {
			return;
		}		
		
		Cursor c = null;
		try{
			c = m_dbInternal.query(TBNAME_LOCALDICTCACHE, 
									new String[]{COLNAME_TYPEVALUE},
									COLUMN_WORD + "=\"" + wdd.m_sbaseword + "\" and " + COLNAME_TYPE + "=\"" + TYPENAME_PINYIN + "\" and " + COLUMN_NETID + "=\"" + wdd.m_inetid + "\"" , 
									null, null, null, COLUMN_ID);
			if(null != c && c.moveToFirst()) {
				String spy = c.getString(0);
				if(!TextUtils.isEmpty(spy) && spy.contentEquals(wdd.m_swordpinyin)){
					return;
				}				
				while(c.moveToNext()) {
					spy = c.getString(0);
					if(!TextUtils.isEmpty(spy) && spy.contentEquals(wdd.m_swordpinyin)){
						return;
					}
				}				
			}
		}
		finally{
			if(null != c) {
				c.close();
			}
		}

        ArrayList<String> sqls = new ArrayList<String>();
        String sql = "";

		for(int i = 0; i < wdd.m_dicts.length; i++) {
            if (null == wdd.m_dicts[i]) {
                continue;
            }

            sql = String.format(INSERT_SQL_LOCALDICTCACHE,
					wdd.m_inetid, wdd.m_ibookid, wdd.m_sbaseword, wdd.m_sdictname, wdd.m_dicts[i].m_dictId, -1,
					TYPENAME_WORDPINYIN, wdd.m_swordpinyin);
            sqls.add(sql);

            if(null != wdd.m_ssduyin && wdd.m_ssduyin.length > 0) {
                StringBuilder sb = new StringBuilder();
                for(int m = 0; m < wdd.m_ssduyin.length; m++) {
                    if(!TextUtils.isEmpty(wdd.m_ssduyin[m])){
                        sb.append(wdd.m_ssduyin[m]);
                        sb.append(";");
                    }
                }
                sql = String.format(INSERT_SQL_LOCALDICTCACHE, +
								wdd.m_inetid, wdd.m_ibookid, wdd.m_sbaseword, wdd.m_sdictname, wdd.m_dicts[i].m_dictId, -1,
						TYPENAME_SOUNDURL, sb.toString());
                sqls.add(sql);
                sb = null;
            }



            sql = String.format(INSERT_SQL_LOCALDICTCACHE,
					wdd.m_inetid, wdd.m_ibookid, wdd.m_sbaseword, wdd.m_sdictname, wdd.m_dicts[i].m_dictId, -1,
					TYPENAME_DICTTYPE, wdd.m_dicts[i].m_type);
            sqls.add(sql);


            sql = String.format(INSERT_SQL_LOCALDICTCACHE,
					wdd.m_inetid, wdd.m_ibookid, wdd.m_sbaseword, wdd.m_sdictname, wdd.m_dicts[i].m_dictId, -1,
					TYPENAME_PINYIN, wdd.m_dicts[i].m_spinyin);
            sqls.add(sql);


			if(!TextUtils.isEmpty(wdd.m_dicts[i].m_sdictimageurl)){
				sql = String.format(INSERT_SQL_LOCALDICTCACHE, +
								wdd.m_inetid, wdd.m_ibookid, wdd.m_sbaseword, wdd.m_sdictname, wdd.m_dicts[i].m_dictId, -1,
						TYPENAME_IMAGEURL, wdd.m_dicts[i].m_sdictimageurl);
				sqls.add(sql);
			}

            if (null != wdd.m_dicts[i].m_items && wdd.m_dicts[i].m_items.length > 0) {
                for (int j = 0; j < wdd.m_dicts[i].m_items.length; j++) {
                    if(!TextUtils.isEmpty(wdd.m_dicts[i].m_items[j].m_sdictimageurl)){
                        sql = String.format(INSERT_SQL_LOCALDICTCACHE, +
										wdd.m_inetid, wdd.m_ibookid, wdd.m_sbaseword, wdd.m_sdictname, wdd.m_dicts[i].m_dictId, wdd.m_dicts[i].m_items[j].m_itemId,
								TYPENAME_IMAGEURL, wdd.m_dicts[i].m_items[j].m_sdictimageurl);
                        sqls.add(sql);
                    }


                    if (null != wdd.m_dicts[i].m_items[j].m_jieshis && wdd.m_dicts[i].m_items[j].m_jieshis.length > 0) {
                        for (int k = 0; k < wdd.m_dicts[i].m_items[j].m_jieshis.length; k++) {
                            sql = String.format(INSERT_SQL_LOCALDICTCACHE,
									wdd.m_inetid, wdd.m_ibookid, wdd.m_sbaseword, wdd.m_sdictname, wdd.m_dicts[i].m_dictId, wdd.m_dicts[i].m_items[j].m_itemId,
									TYPENAME_SHIYI, makedbreadablestring(wdd.m_dicts[i].m_items[j].m_jieshis[k].m_sshiyi));
                            sqls.add(sql);
                            if (null != wdd.m_dicts[i].m_items[j].m_jieshis[k].m_lijus && wdd.m_dicts[i].m_items[j].m_jieshis[k].m_lijus.length > 0) {
                                for (int m = 0; m < wdd.m_dicts[i].m_items[j].m_jieshis[k].m_lijus.length; m++) {
                                    if (!TextUtils.isEmpty(wdd.m_dicts[i].m_items[j].m_jieshis[k].m_lijus[m].m_cizu)) {
                                        sql = String.format(INSERT_SQL_LOCALDICTCACHE,
												wdd.m_inetid, wdd.m_ibookid, wdd.m_sbaseword, wdd.m_sdictname, wdd.m_dicts[i].m_dictId, wdd.m_dicts[i].m_items[j].m_itemId,
												TYPENAME_LIJU, makedbreadablestring(wdd.m_dicts[i].m_items[j].m_jieshis[k].m_lijus[m].m_cizu));
                                        sqls.add(sql);
                                    }
                                    if (!TextUtils.isEmpty(wdd.m_dicts[i].m_items[j].m_jieshis[k].m_lijus[m].m_cizujieshi)) {
                                        sql = String.format(INSERT_SQL_LOCALDICTCACHE,
												wdd.m_inetid, wdd.m_ibookid, wdd.m_sbaseword, wdd.m_sdictname, wdd.m_dicts[i].m_dictId, wdd.m_dicts[i].m_items[j].m_itemId,
												TYPENAME_LIJUJIESHI, makedbreadablestring(wdd.m_dicts[i].m_items[j].m_jieshis[k].m_lijus[m].m_cizujieshi));
                                        sqls.add(sql);
                                    }
                                }
                            }
                            if (null != wdd.m_dicts[i].m_items[j].m_jieshis[k].m_cizus && wdd.m_dicts[i].m_items[j].m_jieshis[k].m_cizus.length > 0) {
                                for (int m = 0; m < wdd.m_dicts[i].m_items[j].m_jieshis[k].m_cizus.length; m++) {
                                    if (!TextUtils.isEmpty(wdd.m_dicts[i].m_items[j].m_jieshis[k].m_cizus[m].m_cizu)) {
                                        sql = String.format(INSERT_SQL_LOCALDICTCACHE,
												wdd.m_inetid, wdd.m_ibookid, wdd.m_sbaseword, wdd.m_sdictname, wdd.m_dicts[i].m_dictId, wdd.m_dicts[i].m_items[j].m_itemId,
												TYPENAME_CIZU, makedbreadablestring(wdd.m_dicts[i].m_items[j].m_jieshis[k].m_cizus[m].m_cizu));
                                        sqls.add(sql);
                                    }
                                    if (!TextUtils.isEmpty(wdd.m_dicts[i].m_items[j].m_jieshis[k].m_cizus[m].m_cizujieshi)) {
                                        sql = String.format(INSERT_SQL_LOCALDICTCACHE,
												wdd.m_inetid, wdd.m_ibookid, wdd.m_sbaseword, wdd.m_sdictname, wdd.m_dicts[i].m_dictId, wdd.m_dicts[i].m_items[j].m_itemId,
												TYPENAME_CIZUJIESHI, makedbreadablestring(wdd.m_dicts[i].m_items[j].m_jieshis[k].m_cizus[m].m_cizujieshi));
                                        sqls.add(sql);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
		dbexcute(sqls);
	}	

	
	public void deleteworddictbynetids(int inetid, String sword, String simageurl) {
		if(null == m_dbInternal) {
			return;
		}
		String sql = String.format(DELETE_SQL_WORDCACHEBYNETIDS, inetid, sword);
		dbexcute(sql);
	}	
	
	public IntDouble[] getwordtextitemrange(int inetid, String sword, String spinyin) {
		if(null == m_dbInternal || TextUtils.isEmpty(sword) || inetid < 0){
			return null;
		}
		
		int ifoundId = -1;
		int ifound = -1;
		ArrayList<StringInt> alpinyins = new ArrayList<StringInt>();
		Cursor c = null;
		try{
			String ssel = COLUMN_WORD + "=\"" + sword + "\" and " + COLUMN_NETID + "=\"" + inetid + "\" and " + COLNAME_TYPE + "=\"" + TYPENAME_WORDPINYIN + "\"";
			c = m_dbInternal.query(TBNAME_LOCALDICTCACHE, 
					new String[]{COLNAME_TYPEVALUE, COLUMN_ID},
					ssel, 
					null, null, null, COLUMN_ID);
			if(null == c || !c.moveToFirst()){
				return null; 
			}
			String spy = c.getString(0);
			if(!TextUtils.isEmpty(spy)){
				alpinyins.add(new StringInt(spy, c.getInt(1)));
				if(spy.contentEquals(spinyin)) {
					ifoundId = c.getInt(1);
					ifound = alpinyins.size() - 1;
				}
			}
			while(c.moveToNext()) {
				spy = c.getString(0);
				if(!TextUtils.isEmpty(spy)){
					alpinyins.add(new StringInt(spy, c.getInt(1)));
					if(spy.contentEquals(spinyin)) {
						ifoundId = c.getInt(1);
						ifound = alpinyins.size() - 1;
					}
				}
			}
			if(ifoundId < 0 || ifound < 0){
				return null;
			}
		}catch(Exception e0){
			TLog.log("DictionaryCNDB : getwordtextitemrange : e0 : " + e0);
		}finally{
			if(null != c) {
				c.close();
			}
			c = null;
		}
		
		try{
			int ihead = ifoundId;
			int itail = (ifound >= alpinyins.size() - 1) ? -1 : (alpinyins.get(ifound + 1).m_iVal2 - 1);
			String ssel = COLUMN_WORD + "=\"" + sword + "\" and " + COLUMN_NETID + "=\"" + inetid + "\" and " + COLUMN_ID + ">=" + ihead;
			if(itail > 0) {
				ssel += " and " + COLUMN_ID + "<=" + itail;
			}
			ArrayList<IntDouble> alidu = new ArrayList<IntDouble>();
			c = m_dbInternal.query(TBNAME_LOCALDICTCACHE, 
									new String[]{COLUMN_ID, COLNAME_ITEMINDEX},
									ssel,
									null, null, null, COLUMN_ID);
			if(null != c && c.moveToFirst()) {
				IntDouble idu = new IntDouble();
				int iitemidx = c.getInt(1);
				alidu.add(idu);
				idu.m_ival1 = c.getInt(0);
				while(c.moveToNext()) {
					if(iitemidx == c.getInt(1)){
						idu.m_ival2 = c.getInt(0);
					}
					else{
						idu = new IntDouble();
						iitemidx = c.getInt(1);
						alidu.add(idu);
						idu.m_ival1 = c.getInt(0);
					}
				}
				int isize = alidu.size();
				if(isize <= 0) {
					return null;
				}
				IntDouble[] idus = new IntDouble[isize];
				for(int i = 0; i < isize; i++) {
					idus[i] = alidu.get(i);
				}
				alidu.clear();
				return idus;
			}
		}
		catch(Exception e){
			TLog.log("DictionaryCNDB : getwordtextitemrange : e : " + e);
		}
		finally{
			if(null != c) {
				c.close();
			}
		}
		return null;
	}

    public IntDouble[] getworddictrangewithoutnetidold(String sword, String spinyin) {
        if(null == m_dbInternal || TextUtils.isEmpty(sword)){
            return null;
        }

        int ifoundId = -1;
        int ifound = -1;
        ArrayList<StringInt> alpinyins = new ArrayList<StringInt>();
        Cursor c = null;
        try{
            String ssel = COLUMN_WORD + "=\"" + sword + "\" and " + COLNAME_TYPE + "=\"" + TYPENAME_WORDPINYIN + "\"";
            c = m_dbInternal.query(TBNAME_LOCALDICTCACHE,
                    new String[]{COLNAME_TYPEVALUE, COLUMN_ID},
                    ssel,
                    null, null, null, COLUMN_ID);
            if(null == c || !c.moveToFirst()){
                return null;
            }
            String spy = c.getString(0);
            if(!TextUtils.isEmpty(spy)){
                //alpinyins.add(new StringInt(spy, c.getInt(1)));
                if(spy.contentEquals(spinyin)) {
                    alpinyins.add(new StringInt(spy, c.getInt(1)));
                    if (ifoundId <= 0) ifoundId = c.getInt(1);
                    ifound = alpinyins.size() - 1;
                }
            }
            while(c.moveToNext()) {
                spy = c.getString(0);
                if(!TextUtils.isEmpty(spy)){
                    //alpinyins.add(new StringInt(spy, c.getInt(1)));
                    if(spy.contentEquals(spinyin)) {
                        alpinyins.add(new StringInt(spy, c.getInt(1)));
                        if (ifoundId <= 0) ifoundId = c.getInt(1);
                        ifound = alpinyins.size() - 1;
                    }
                }
            }
            if(ifoundId < 0 || ifound < 0){
                return null;
            }
        }catch(Exception e0){
            TLog.log("DictionaryCNDB : getwordtextitemrange : e0 : " + e0);
        }finally{
            if(null != c) {
                c.close();
            }
            c = null;
        }

        try{
            int ihead = ifoundId;
           // int itail = (ifound >= alpinyins.size() - 1) ? -1 : (alpinyins.get(ifound + 1).m_iVal2 - 1);
            String ssel = COLUMN_WORD + "=\"" + sword + "\" and " + COLUMN_ID + ">=" + ihead;
           /* if(itail > 0) {
                ssel += " and " + COLUMN_ID + "<=" + itail;
            }*/
            ArrayList<IntDouble> alidu = new ArrayList<IntDouble>();
            c = m_dbInternal.query(TBNAME_LOCALDICTCACHE,
                    new String[]{COLUMN_ID, COLNAME_DICTINDEX},
                    ssel,
                    null, null, null, COLUMN_ID);
            if(null != c && c.moveToFirst()) {
                IntDouble idu = new IntDouble();
                int iitemidx = c.getInt(1);
                alidu.add(idu);
                idu.m_ival1 = c.getInt(0);
                while(c.moveToNext()) {
                    if(iitemidx == c.getInt(1)){
                        idu.m_ival2 = c.getInt(0);
                    }
                    else{
                        idu = new IntDouble();
                        iitemidx = c.getInt(1);
                        alidu.add(idu);
                        idu.m_ival1 = c.getInt(0);
                    }
                }
                int isize = alidu.size();
                if(isize <= 0) {
                    return null;
                }
                IntDouble[] idus = new IntDouble[isize];
                for(int i = 0; i < isize; i++) {
                    idus[i] = alidu.get(i);
                }
                alidu.clear();
                return idus;
            }
        }
        catch(Exception e){
            TLog.log("DictionaryCNDB : getwordtextitemrange : e : " + e);
        }
        finally{
            if(null != c) {
                c.close();
            }
        }
        return null;
    }


    public IntDouble[] getworddictrangewithoutnetid(String sword, String spinyin) {
        if(null == m_dbInternal || TextUtils.isEmpty(sword)){
            return null;
        }

        int ifoundId = -1;
        int ifound = -1;
        ArrayList<StringInt> alpinyins = new ArrayList<StringInt>();
        Cursor c = null;
        try{
            String ssel = COLUMN_WORD + "=\"" + sword + "\" and " + COLNAME_TYPE + "=\"" + TYPENAME_WORDPINYIN + "\"";
            c = m_dbInternal.query(TBNAME_LOCALDICTCACHE,
                    new String[]{COLNAME_TYPEVALUE, COLUMN_ID},
                    ssel,
                    null, null, null, COLUMN_ID);
            if(null == c || !c.moveToFirst()){
                return null;
            }
            String spy = c.getString(0);
            if(!TextUtils.isEmpty(spy)){
                alpinyins.add(new StringInt(spy, c.getInt(1)));
                if(spy.contentEquals(spinyin)) {
                    ifoundId = c.getInt(1);
                    ifound = alpinyins.size() - 1;
                }
            }
            while(c.moveToNext()) {
                spy = c.getString(0);
                if(!TextUtils.isEmpty(spy)){
                    alpinyins.add(new StringInt(spy, c.getInt(1)));
                    if(spy.contentEquals(spinyin)) {
                        if (ifoundId < 0)  ifoundId = c.getInt(1);
                        ifound = alpinyins.size() - 1;
                    }
                }
            }
            if(ifoundId < 0 || ifound < 0){
                return null;
            }
        }catch(Exception e0){
            TLog.log("DictionaryCNDB : getwordtextitemrange : e0 : " + e0);
        }finally{
            if(null != c) {
                c.close();
            }
            c = null;
        }

        try{
            int ihead = ifoundId;
            int itail = (ifound >= alpinyins.size() - 1) ? -1 : (alpinyins.get(ifound + 1).m_iVal2 - 1);
            String ssel = COLUMN_WORD + "=\"" + sword + "\" and " + COLUMN_ID + ">=" + ihead;
            if(itail > 0) {
                ssel += " and " + COLUMN_ID + "<=" + itail;
            }
            ArrayList<IntDouble> alidu = new ArrayList<IntDouble>();
            c = m_dbInternal.query(TBNAME_LOCALDICTCACHE,
                    new String[]{COLUMN_ID, COLNAME_DICTINDEX},
                    ssel,
                    null, null, null, COLUMN_ID);
            if(null != c && c.moveToFirst()) {
                IntDouble idu = new IntDouble();
                int iitemidx = c.getInt(1);
                alidu.add(idu);
                idu.m_ival1 = c.getInt(0);
                while(c.moveToNext()) {
                    if(iitemidx == c.getInt(1)){
                        idu.m_ival2 = c.getInt(0);
                    }
                    else{
                        idu = new IntDouble();
                        iitemidx = c.getInt(1);
                        alidu.add(idu);
                        idu.m_ival1 = c.getInt(0);
                    }
                }
                int isize = alidu.size();
                if(isize <= 0) {
                    return null;
                }
                IntDouble[] idus = new IntDouble[isize];
                for(int i = 0; i < isize; i++) {
                    idus[i] = alidu.get(i);
                }
                alidu.clear();
                return idus;
            }
        }
        catch(Exception e){
            TLog.log("DictionaryCNDB : getwordtextitemrange : e : " + e);
        }
        finally{
            if(null != c) {
                c.close();
            }
        }
        return null;
    }


    public IntDouble[] getwordtextitemrangewithoutnetid(String sword, String spinyin) {
		if(null == m_dbInternal || TextUtils.isEmpty(sword)){
			return null;
		}
		
		int ifoundId = -1;
		int ifound = -1;
		ArrayList<StringInt> alpinyins = new ArrayList<StringInt>();
		Cursor c = null;
		try{
			String ssel = COLUMN_WORD + "=\"" + sword + "\" and " + COLNAME_TYPE + "=\"" + TYPENAME_WORDPINYIN + "\"";
			c = m_dbInternal.query(TBNAME_LOCALDICTCACHE, 
					new String[]{COLNAME_TYPEVALUE, COLUMN_ID},
					ssel, 
					null, null, null, COLUMN_ID);
			if(null == c || !c.moveToFirst()){
				return null; 
			}
			String spy = c.getString(0);
			if(!TextUtils.isEmpty(spy)){
				alpinyins.add(new StringInt(spy, c.getInt(1)));
				if(spy.contentEquals(spinyin)) {
					ifoundId = c.getInt(1);
					ifound = alpinyins.size() - 1;
				}
			}
			while(c.moveToNext()) {
				spy = c.getString(0);
				if(!TextUtils.isEmpty(spy)){
					alpinyins.add(new StringInt(spy, c.getInt(1)));
					if(spy.contentEquals(spinyin)) {
						ifoundId = c.getInt(1);
						ifound = alpinyins.size() - 1;
					}
				}
			}
			if(ifoundId < 0 || ifound < 0){
				return null;
			}
		}catch(Exception e0){
			TLog.log("DictionaryCNDB : getwordtextitemrange : e0 : " + e0);
		}finally{
			if(null != c) {
				c.close();
			}
			c = null;
		}
		
		try{
			int ihead = ifoundId;
			int itail = (ifound >= alpinyins.size() - 1) ? -1 : (alpinyins.get(ifound + 1).m_iVal2 - 1);
			String ssel = COLUMN_WORD + "=\"" + sword + "\" and " + COLUMN_ID + ">=" + ihead;
			if(itail > 0) {
				ssel += " and " + COLUMN_ID + "<=" + itail;
			}
			ArrayList<IntDouble> alidu = new ArrayList<IntDouble>();
			c = m_dbInternal.query(TBNAME_LOCALDICTCACHE, 
									new String[]{COLUMN_ID, COLNAME_ITEMINDEX},
									ssel,
									null, null, null, COLUMN_ID);
			if(null != c && c.moveToFirst()) {
				IntDouble idu = new IntDouble();
				int iitemidx = c.getInt(1);
				alidu.add(idu);
				idu.m_ival1 = c.getInt(0);
				while(c.moveToNext()) {
					if(iitemidx == c.getInt(1)){
						idu.m_ival2 = c.getInt(0);
					}
					else{
						idu = new IntDouble();
						iitemidx = c.getInt(1);
						alidu.add(idu);
						idu.m_ival1 = c.getInt(0);
					}
				}
				int isize = alidu.size();
				if(isize <= 0) {
					return null;
				}
				IntDouble[] idus = new IntDouble[isize];
				for(int i = 0; i < isize; i++) {
					idus[i] = alidu.get(i);
				}
				alidu.clear();
				return idus;
			}
		}
		catch(Exception e){
			TLog.log("DictionaryCNDB : getwordtextitemrange : e : " + e);
		}
		finally{
			if(null != c) {
				c.close();
			}
		}
		return null;
	}

    public IntDouble[] getwordictitemrangeswithoutnetid(int idictid, String sword, int ihead, int itail){
        if(null == m_dbInternal || TextUtils.isEmpty(sword)){
            return null;
        }
        Cursor c = null;
        try{
            String ssel = COLUMN_WORD + "=\"" + sword + "\" and " + COLNAME_DICTINDEX + "=\"" + idictid + "\"";
            ssel += " and " + COLUMN_ID + " >= " + ihead + " and " + COLUMN_ID + "<=" + itail;
            c = m_dbInternal.query(TBNAME_LOCALDICTCACHE,
                    new String[]{COLUMN_ID, COLNAME_ITEMINDEX},
                    ssel,
                    null, null, null, COLUMN_ID);
            if(null != c && c.moveToFirst()) {
                ArrayList<IntDouble> iduslist = new ArrayList<IntDouble>();
                int curitemid = c.getInt(1);
                IntDouble  item = null;
                if (curitemid >= 0) {
                    item = new IntDouble();
                    item.m_ival1 = c.getInt(0);
                    item.m_ival2 = c.getInt(0);
                    iduslist.add(item);
                }

                while(c.moveToNext()) {
                    int newitemid = c.getInt(1);
                    if (newitemid != curitemid && newitemid >= 0) {
                        item = new IntDouble();
                        item.m_ival1 = c.getInt(0);
                        item.m_ival2 = c.getInt(0);
                        iduslist.add(item);
                    } else if (item != null) {
                        item.m_ival2 = c.getInt(0);
                    }

                    curitemid = newitemid;

                }
                if(null != c) {
                    c.close();
                }
                c = null;
                if (iduslist != null) {
                    int size = iduslist.size();
                    if (size > 0) {
                        IntDouble[] idus = new IntDouble[size];
                        for (int i = 0; i < size; i++) {
                            idus[i] = iduslist.get(i);
                        }
                        iduslist.clear();

                        return idus;
                    }
                }
            }
        }
        finally{
            if(null != c) {
                c.close();
            }
        }
        return null;
    }
	
	public String getworddictname(String sword, int inetid){
		if(null == m_dbInternal || TextUtils.isEmpty(sword) || inetid < 0){
			return null;
		}
		Cursor c = null;
		try{
			c = m_dbInternal.query(TBNAME_LOCALDICTCACHE, 
									new String[]{COLNAME_DICTNAME},
									COLUMN_WORD + "=\"" + sword + "\" and " + COLUMN_NETID + "=\"" + inetid + "\"", 
									null, null, null, COLUMN_ID);
			if(null != c && c.moveToFirst()) {
				return c.getString(0);
			}
		}
		finally{
			if(null != c) {
				c.close();
			}
		}
		return null;
	}
	
	public String getworddictnamewithoutnetid(String sword, String swordpinyin){
		if(null == m_dbInternal || TextUtils.isEmpty(sword) || TextUtils.isEmpty(swordpinyin)){
			return null;
		}
		Cursor c = null;
		try{
			c = m_dbInternal.query(TBNAME_LOCALDICTCACHE, 
									new String[]{COLNAME_DICTNAME, COLNAME_TYPEVALUE},
									COLUMN_WORD + "=\"" + sword + "\" and " + COLNAME_TYPE + "=\"" + TYPENAME_WORDPINYIN + "\"", 
									null, null, null, COLUMN_ID);
			if(null != c && c.moveToFirst()) {
				String sgetpinyin = c.getString(1);
				if(!TextUtils.isEmpty(sgetpinyin) && sgetpinyin.contentEquals(swordpinyin)){
					return c.getString(0);
				}
			}
		}
		finally{
			if(null != c) {
				c.close();
			}
		}
		return null;
	}
	
	public String getworditemshiyipinyin(int inetid, int iitemid, String sword, int ihead, int itail){
		if(null == m_dbInternal || TextUtils.isEmpty(sword) || inetid < 0){
			return null;
		}
		Cursor c = null;
		try{
			c = m_dbInternal.query(TBNAME_LOCALDICTCACHE, 
									new String[]{COLNAME_TYPEVALUE, COLUMN_ID},
									COLUMN_WORD + "=\"" + sword + "\" and " + COLUMN_NETID + "=\"" + inetid + "\" and " + COLNAME_ITEMINDEX + "=\"" + iitemid + "\" and " + COLNAME_TYPE + "=\"" + TYPENAME_PINYIN + "\"", 
									null, null, null, COLUMN_ID);
			if(null != c && c.moveToFirst()) {
				String s = c.getString(0);
				int id = c.getInt(1);
				if(id >= ihead && id <= itail){
					return s;
				}
			}
			while(c.moveToNext()) {
				String s = c.getString(0);
				int id = c.getInt(1);
				if(id >= ihead && id <= itail){
					return s;
				}
			}
		}
		finally{
			if(null != c) {
				c.close();
			}
		}
		return null;	
	}

	public String getworditemshiyipinyinwithoutnetid(int iitemid, String sword, int ihead, int itail){
		if(null == m_dbInternal || TextUtils.isEmpty(sword)){
			return null;
		}
		Cursor c = null;
		try{
			c = m_dbInternal.query(TBNAME_LOCALDICTCACHE, 
									new String[]{COLNAME_TYPEVALUE, COLUMN_ID},
									COLUMN_WORD + "=\"" + sword + "\" and " + COLNAME_ITEMINDEX + "=\"" + iitemid + "\" and " + COLNAME_TYPE + "=\"" + TYPENAME_PINYIN + "\"", 
									null, null, null, COLUMN_ID);
			if(null != c && c.moveToFirst()) {
				String s = c.getString(0);
				int id = c.getInt(1);
				if(id >= ihead && id <= itail){
					return s;
				}
			}
			while(c.moveToNext()) {
				String s = c.getString(0);
				int id = c.getInt(1);
				if(id >= ihead && id <= itail){
					return s;
				}
			}
		}
		finally{
			if(null != c) {
				c.close();
			}
		}
		return null;	
	}

    public String getworditemshiyipinyinwithoutnetid(int idictid, int iitemid, String sword, int ihead, int itail){
        if(null == m_dbInternal || TextUtils.isEmpty(sword)){
            return null;
        }
        Cursor c = null;
        try{
            c = m_dbInternal.query(TBNAME_LOCALDICTCACHE,
                    new String[]{COLNAME_TYPEVALUE, COLUMN_ID},
                    COLUMN_WORD + "=\"" + sword + "\" and " + COLNAME_DICTINDEX + "=\"" + idictid + "\" and " + COLNAME_ITEMINDEX + "=\"" + iitemid + "\" and " + COLNAME_TYPE + "=\"" + TYPENAME_PINYIN + "\"",
                    null, null, null, COLUMN_ID);
            if(null != c && c.moveToFirst()) {
                String s = c.getString(0);
                int id = c.getInt(1);
                if(id >= ihead && id <= itail){
                    return s;
                }
            }
            while(c.moveToNext()) {
                String s = c.getString(0);
                int id = c.getInt(1);
                if(id >= ihead && id <= itail){
                    return s;
                }
            }
        }
        finally{
            if(null != c) {
                c.close();
            }
        }
        return null;
    }
	
	public String getworditemshiyiimageurl(int inetid, int iitemid, String sword){
		if(null == m_dbInternal || TextUtils.isEmpty(sword) || inetid < 0){
			return null;
		}
		Cursor c = null;
		try{
			c = m_dbInternal.query(TBNAME_LOCALDICTCACHE, 
									new String[]{COLNAME_TYPEVALUE},
									COLUMN_WORD + "=\"" + sword + "\" and " + COLUMN_NETID + "=\"" + inetid + "\" and " + COLNAME_ITEMINDEX + "=\"" + iitemid + "\" and " + COLNAME_TYPE + "=\"" + TYPENAME_IMAGEURL + "\"", 
									null, null, null, COLUMN_ID);
			if(null != c && c.moveToFirst()) {
				return c.getString(0);
			}
		}
		finally{
			if(null != c) {
				c.close();
			}
		}
		return null;	
	}
	
	public String getworditemshiyiimageurlwithoutnetid(int iitemid, String sword, int ihead, int itail){
		if(null == m_dbInternal || TextUtils.isEmpty(sword)){
			return null;
		}
		Cursor c = null;
		try{
			String ssel = COLUMN_WORD + "=\"" + sword + "\" and " + COLNAME_ITEMINDEX + "=\"" + iitemid + "\" and " + COLNAME_TYPE + "=\"" + TYPENAME_IMAGEURL +
					"\" and " + COLUMN_ID + ">=\"" + ihead + "\" and " + COLUMN_ID + "<=\"" + itail + "\"";
			c = m_dbInternal.query(TBNAME_LOCALDICTCACHE, 
									new String[]{COLNAME_TYPEVALUE},
									ssel, 
									null, null, null, COLUMN_ID);
			if(null != c && c.moveToFirst()) {
				return c.getString(0);
			}
		}
		finally{
			if(null != c) {
				c.close();
			}
		}
		return null;	
	}

    public String getworditemshiyiimageurlwithoutnetid(int idictid, int iitemid, String sword, int ihead, int itail){
        if(null == m_dbInternal || TextUtils.isEmpty(sword)){
            return null;
        }
        Cursor c = null;
        try{
            String ssel = COLUMN_WORD + "=\"" + sword + "\" and " + COLNAME_DICTINDEX + "=\"" + idictid + "\" and " + COLNAME_ITEMINDEX + "=\"" + iitemid + "\" and " + COLNAME_TYPE + "=\"" + TYPENAME_IMAGEURL +
                    "\" and " + COLUMN_ID + ">=\"" + ihead + "\" and " + COLUMN_ID + "<=\"" + itail + "\"";
            c = m_dbInternal.query(TBNAME_LOCALDICTCACHE,
                    new String[]{COLNAME_TYPEVALUE},
                    ssel,
                    null, null, null, COLUMN_ID);
            if(null != c && c.moveToFirst()) {
                return c.getString(0);
            }
        }
        finally{
            if(null != c) {
                c.close();
            }
        }
        return null;
    }





    public String getworditemduyinurl(int inetid, int iitemid, String sword, int ihead, int itail){
		if(null == m_dbInternal || TextUtils.isEmpty(sword) || inetid < 0){
			return null;
		}
		Cursor c = null;
		try{
			String ssel = COLUMN_WORD + "=\"" + sword + "\" and " + COLUMN_NETID + "=\"" + inetid + "\" and " + COLNAME_ITEMINDEX + "=\"" + iitemid + "\" and " + COLNAME_TYPE + "=\"" + TYPENAME_SOUNDURL +
					"\" and " + COLUMN_ID + ">=\"" + ihead + "\" and " + COLUMN_ID + "<=\"" + itail + "\"";
			c = m_dbInternal.query(TBNAME_LOCALDICTCACHE,
									new String[]{COLNAME_TYPEVALUE},
									ssel, 
									null, null, null, COLUMN_ID);
			if(null != c && c.moveToFirst()) {
				return c.getString(0);
			}
		}
		finally{
			if(null != c) {
				c.close();
			}
		}
		return null;	
	}

    public String getworddictduyinurlwithoutnetid(int dictid, String sword, int ihead, int itail){
        if(null == m_dbInternal || TextUtils.isEmpty(sword)){
            return null;
        }
        Cursor c = null;
        try{
            String ssel = COLUMN_WORD + "=\"" + sword + "\" and " + COLNAME_DICTINDEX + "=\"" + dictid + "\" and " + COLNAME_TYPE + "=\"" + TYPENAME_SOUNDURL + "\"";
            ssel += " and " + COLUMN_ID + " >= " + ihead + " and " + COLUMN_ID + "<=" + itail;
            c = m_dbInternal.query(TBNAME_LOCALDICTCACHE,
                    new String[]{COLNAME_TYPEVALUE},
                    ssel,
                    null, null, null, COLUMN_ID);
            if(null != c && c.moveToFirst()) {
                return c.getString(0);
            }
        }
        finally{
            if(null != c) {
                c.close();
            }
        }
        return null;
    }

    public String getworddicttypewithoutnetid(int dictid, String sword, int ihead, int itail){
        if(null == m_dbInternal || TextUtils.isEmpty(sword)){
            return null;
        }
        Cursor c = null;
        try{
            String ssel = COLUMN_WORD + "=\"" + sword + "\" and " + COLNAME_DICTINDEX + "=\"" + dictid + "\" and " + COLNAME_TYPE + "=\"" + TYPENAME_DICTTYPE + "\"";
            ssel += " and " + COLUMN_ID + " >= " + ihead + " and " + COLUMN_ID + "<=" + itail;
            c = m_dbInternal.query(TBNAME_LOCALDICTCACHE,
                    new String[]{COLNAME_TYPEVALUE},
                    ssel,
                    null, null, null, COLUMN_ID);
            if(null != c && c.moveToFirst()) {
                return c.getString(0);
            }
        }
        finally{
            if(null != c) {
                c.close();
            }
        }
        return null;
    }


    public String getworditemduyinurlwithoutnetid(int iitemid, String sword, int ihead, int itail){
		if(null == m_dbInternal || TextUtils.isEmpty(sword)){
			return null;
		}
		Cursor c = null;
		try{
			String ssel = COLUMN_WORD + "=\"" + sword + "\" and " + COLNAME_ITEMINDEX + "=\"" + iitemid + "\" and " + COLNAME_TYPE + "=\"" + TYPENAME_SOUNDURL + "\"";
			ssel += " and " + COLUMN_ID + " >= " + ihead + " and " + COLUMN_ID + "<=" + itail;
			c = m_dbInternal.query(TBNAME_LOCALDICTCACHE,
									new String[]{COLNAME_TYPEVALUE},
									ssel,
									null, null, null, COLUMN_ID);
			if(null != c && c.moveToFirst()) {
				return c.getString(0);
			}
		}
		finally{
			if(null != c) {
				c.close();
			}
		}
		return null;
	}
		
	public IntDouble[] getworditemshiyiranges(int inetid, int iitemid, String sword, int ihead, int itail){
		if(null == m_dbInternal || TextUtils.isEmpty(sword) || inetid < 0){
			return null;
		}
		Cursor c = null;
		try{
			String ssel = COLUMN_WORD + "=\"" + sword + "\" and " + COLUMN_NETID + "=\"" + inetid + "\" and " + COLNAME_ITEMINDEX + "=\"" + iitemid + "\" and " + COLNAME_TYPE + "=\"" + TYPENAME_SHIYI + "\"";
			ssel += " and " + COLUMN_ID + " >= " + ihead + " and " + COLUMN_ID + "<=" + itail;
			c = m_dbInternal.query(TBNAME_LOCALDICTCACHE, 
									new String[]{COLUMN_ID},
									ssel,
									null, null, null, COLUMN_ID);
			if(null != c && c.moveToFirst()) {
				IntDouble idus[] = new IntDouble[c.getCount()];
				int idx = 0;
				idus[idx] = new IntDouble();
				idus[idx++].m_ival1 = c.getInt(0);
				while(c.moveToNext()) {
					idus[idx] = new IntDouble();
					idus[idx++].m_ival1 = c.getInt(0);
				}
				if(null != c) {
					c.close();
				}
				c = null;
				for(int i = 0; i < idus.length - 1; i++) {
					idus[i].m_ival2 = idus[i + 1].m_ival1 - 1;
				}
				idus[idus.length - 1].m_ival2 = itail;
				return idus;
			}
		}
		finally{
			if(null != c) {
				c.close();
			}
		}
		return null;
	}

    public IntDouble[] getworditemrangeswithoutnetid(int dictid, String sword, int ihead, int itail){
        if(null == m_dbInternal || TextUtils.isEmpty(sword)){
            return null;
        }
        Cursor c = null;
        try{
            String ssel = COLUMN_WORD + "=\"" + sword + "\" and " + COLNAME_DICTINDEX + "=\"" + dictid + "\"";
            ssel += " and " + COLUMN_ID + " >= " + ihead + " and " + COLUMN_ID + "<=" + itail;
            ArrayList<IntDouble> alidu = new ArrayList<IntDouble>();
            c = m_dbInternal.query(TBNAME_LOCALDICTCACHE,
                    new String[]{COLUMN_ID, COLNAME_ITEMINDEX},
                    ssel,
                    null, null, null, COLUMN_ID);
            if(null != c && c.moveToFirst()) {
                if(null != c && c.moveToFirst()) {
                    IntDouble idu = new IntDouble();
                    int iitemidx = c.getInt(1);
                    alidu.add(idu);
                    idu.m_ival1 = c.getInt(0);
                    while(c.moveToNext()) {
                        if(iitemidx == c.getInt(1)){
                            idu.m_ival2 = c.getInt(0);
                        }
                        else{
                            idu = new IntDouble();
                            iitemidx = c.getInt(1);
                            alidu.add(idu);
                            idu.m_ival1 = c.getInt(0);
                        }
                    }
                    int isize = alidu.size();
                    if(isize <= 0) {
                        return null;
                    }
                    IntDouble[] idus = new IntDouble[isize];
                    for(int i = 0; i < isize; i++) {
                        idus[i] = alidu.get(i);
                    }
                    alidu.clear();
                    return idus;
                }
            }
        }
        finally{
            if(null != c) {
                c.close();
            }
        }
        return null;
    }

	public IntDouble[] getworditemshiyirangeswithoutnetid(int iitemid, String sword, int ihead, int itail){
		if(null == m_dbInternal || TextUtils.isEmpty(sword)){
			return null;
		}
		Cursor c = null;
		try{
			String ssel = COLUMN_WORD + "=\"" + sword + "\" and " + COLNAME_ITEMINDEX + "=\"" + iitemid + "\" and " + COLNAME_TYPE + "=\"" + TYPENAME_SHIYI + "\"";
			ssel += " and " + COLUMN_ID + " >= " + ihead + " and " + COLUMN_ID + "<=" + itail;
			c = m_dbInternal.query(TBNAME_LOCALDICTCACHE, 
									new String[]{COLUMN_ID},
									ssel,
									null, null, null, COLUMN_ID);
			if(null != c && c.moveToFirst()) {
				IntDouble idus[] = new IntDouble[c.getCount()];
				int idx = 0;
				idus[idx] = new IntDouble();
				idus[idx++].m_ival1 = c.getInt(0);
				while(c.moveToNext()) {
					idus[idx] = new IntDouble();
					idus[idx++].m_ival1 = c.getInt(0);
				}
				if(null != c) {
					c.close();
				}
				c = null;
				for(int i = 0; i < idus.length - 1; i++) {
					idus[i].m_ival2 = idus[i + 1].m_ival1 - 1;
				}
				idus[idus.length - 1].m_ival2 = itail;
				return idus;
			}
		}
		finally{
			if(null != c) {
				c.close();
			}
		}
		return null;
	}

    public IntDouble[] getworditemshiyirangeswithoutnetid(int idictid, int iitemid, String sword, int ihead, int itail){
        if(null == m_dbInternal || TextUtils.isEmpty(sword)){
            return null;
        }
        Cursor c = null;
        try{
            String ssel = COLUMN_WORD + "=\"" + sword + "\" and " + COLNAME_DICTINDEX + "=\"" + idictid + "\" and " + COLNAME_ITEMINDEX + "=\"" + iitemid + "\" and " + COLNAME_TYPE + "=\"" + TYPENAME_SHIYI + "\"";
            ssel += " and " + COLUMN_ID + " >= " + ihead + " and " + COLUMN_ID + "<=" + itail;
            c = m_dbInternal.query(TBNAME_LOCALDICTCACHE,
                    new String[]{COLUMN_ID},
                    ssel,
                    null, null, null, COLUMN_ID);
            if(null != c && c.moveToFirst()) {
                IntDouble idus[] = new IntDouble[c.getCount()];
                int idx = 0;
                idus[idx] = new IntDouble();
                idus[idx++].m_ival1 = c.getInt(0);
                while(c.moveToNext()) {
                    idus[idx] = new IntDouble();
                    idus[idx++].m_ival1 = c.getInt(0);
                }
                if(null != c) {
                    c.close();
                }
                c = null;
                for(int i = 0; i < idus.length - 1; i++) {
                    idus[i].m_ival2 = idus[i + 1].m_ival1 - 1;
                }
                idus[idus.length - 1].m_ival2 = itail;
                return idus;
            }
        }
        finally{
            if(null != c) {
                c.close();
            }
        }
        return null;
    }

	
	public String getwordshiyibydatabaseid(int idatabaseid){
		if(null == m_dbInternal){
			return null;
		}
		Cursor c = null;
		try{
			c = m_dbInternal.query(TBNAME_LOCALDICTCACHE, 
									new String[]{COLNAME_TYPEVALUE},
									COLUMN_ID + "=\"" + idatabaseid + "\"", 
									null, null, null, null);
			if(null != c && c.moveToFirst()) {
				return makeUIreadablestring(c.getString(0));
			}
		}
		catch(Exception e){
			TLog.log("DictionaryCNDB : getwordshiyibydatabaseid : e : " + e);
		}
		finally{
			if(null != c) {
				c.close();
			}
		}
		return null;
	}
	
	public boolean getwordspecificcizusandliju(int idatabasefrom, int idatabaseto, ArrayList<WordDictdescData.ParcelableCizu> aloutcizu, ArrayList<WordDictdescData.ParcelableCizu> aloutliju){
		if(idatabasefrom > idatabaseto || null == aloutcizu || null == aloutliju) {
			return false;
		}
		ArrayList<WordDictdescData.ParcelableCizu> alpcizu = aloutcizu;
		alpcizu.clear();
		ArrayList<WordDictdescData.ParcelableCizu> alpliju = aloutliju;
		alpliju.clear();
		for(int i = idatabasefrom; i <= idatabaseto; i++) {
			Cursor c = null;
			try{
				c = m_dbInternal.query(TBNAME_LOCALDICTCACHE, 
										new String[]{COLNAME_TYPE, COLNAME_TYPEVALUE},
										COLUMN_ID + "=\"" + i + "\"", 
										null, null, null, null);
				if(null != c && c.moveToFirst()) {
					String stype = c.getString(0);
					if(stype.contentEquals(TYPENAME_CIZU)) {
						WordDictdescData.ParcelableCizu pc = new WordDictdescData.ParcelableCizu();
						alpcizu.add(pc);
						pc.m_cizu = makeUIreadablestring(c.getString(1));
					}
					else if(stype.contentEquals(TYPENAME_CIZUJIESHI)){
						int iupper = alpcizu.size() - 1;
						if(iupper >= 0) {
							alpcizu.get(iupper).m_cizujieshi = makeUIreadablestring(c.getString(1)); 
						}
					}
					else if(stype.contentEquals(TYPENAME_LIJU)){
						WordDictdescData.ParcelableCizu pc = new WordDictdescData.ParcelableCizu();
						alpliju.add(pc);
						pc.m_cizu = makeUIreadablestring(c.getString(1));
					}
					else if(stype.contentEquals(TYPENAME_LIJUJIESHI)){
						int iupper = alpliju.size() - 1;
						if(iupper >= 0) {
							alpliju.get(iupper).m_cizujieshi = makeUIreadablestring(c.getString(1)); 
						}
					}
				}
			}
			catch(Exception e){
				TLog.log("DictionaryCNDB : getwordspecificcizusandliju : e : " + e);
			}
			finally{
				if(null != c) {
					c.close();
				}
			}
		}
		return true;
	}

	private void dbexcute(ArrayList<String> sqls) {
		if(null == m_dbInternal || null == sqls || sqls.size() <= 0) {
			return;
		} 
        try {   
        	m_dbInternal.beginTransaction(); 
        	for(String sql : sqls) {
        		if(null != sql) {
        	        try {   
                	m_dbInternal.execSQL(sql); }
        	        catch(Exception e){
        	        }
        		}
        	} 
        	m_dbInternal.setTransactionSuccessful();   
        } finally {   
        	m_dbInternal.endTransaction();   
        }  
	}
	
	private void dbexcute(String sql) {
		if(null == m_dbInternal) {
			return;
		} 
        try {   
        	m_dbInternal.beginTransaction();   
        	m_dbInternal.execSQL(sql);   
        	m_dbInternal.setTransactionSuccessful();   
        } finally {   
        	m_dbInternal.endTransaction();   
        }  
	}

	private void dbexcute(String sql, Object[] args) {
		if(null == m_dbInternal) {
			return;
		} 
        try {   
        	m_dbInternal.beginTransaction();   
        	m_dbInternal.execSQL(sql, args);   
        	m_dbInternal.setTransactionSuccessful();   
        } finally {   
        	m_dbInternal.endTransaction();   
        }  
	}
	
	private String makedbreadablestring(String s) {
		if(TextUtils.isEmpty(s)) {
			return s;
		}
		return s.replace("\"", "&quot;");
	}
	
	private String makeUIreadablestring(String s) {
		if(TextUtils.isEmpty(s)) {
			return s;
		}
		return s.replace("&quot;", "\"");
	}
	
	/* ================================ */
	
	private static class DatabaseHelper extends SQLiteOpenHelper {
		public DatabaseHelper(Context context, String sFilepath) {
			super(context, sFilepath, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db)  {
			db.execSQL(CREATE_SQL_LOCALDICTCACHE);		
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			final String strDrop = "DROP TABLE IF EXISTS ";
			db.execSQL(strDrop + CREATE_SQL_LOCALDICTCACHE);
			onCreate(db);
		}
	}	
	
}
